/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.EmployeeDao;
import entity.Employee;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author rmartinez
 */
@WebServlet(name = "QueryServlet", urlPatterns = {"/EmployeeList", "/ListByName"})
public class QueryServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response){
        String path = request.getServletPath();
        try {
            if (path.equals("/EmployeeList")) {
                employeeList(request, response);
            }
            else if (path.equals("/ListByName")) {
                listByName(request, response);
            }
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
    }

    private void employeeList(HttpServletRequest request, HttpServletResponse response) {
        try {
            EmployeeDao obj = new EmployeeDao();
            List<Employee> empList = obj.employeesList();
            request.setAttribute("list", empList);
            RequestDispatcher rd = request.getRequestDispatcher("EmployeeList.jsp");
            rd.forward(request, response);
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
    }
    
    private void listByName(HttpServletRequest request, HttpServletResponse response) {
        try {
            EmployeeDao obj = new EmployeeDao();
            Employee r = new Employee();
            String name = request.getParameter("txtName");
            r.setFirst_name(name);
            List<Employee> empList = obj.listByCriteria(r);
            request.setAttribute("list", empList);
            RequestDispatcher rd = request.getRequestDispatcher("EmployeesByName.jsp");
            rd.forward(request, response);
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
    }
}
